<?php

namespace app\merchant\controller\finance;

use app\merchant\controller\Base;
use app\merchant\model\Order as OrderModel;
use app\merchant\model\dealer\Order as DealerOrderModel;
use app\common\model\divide\Receive as ReceiveModel;
use think\Db;


/**
 * 订单信息
 * Class Order
 * @package app\merchant\controller\statistics
 */
class Order extends Base
{
    /**
     * 全部订单
     * @return mixed
     * @throws \think\Exception
     */
    public function all()
    {
        // 订单列表
        $model = new OrderModel;
        $list = $model->getAllList($this->request->param());
        return $this->renderSuccess($list,"查询成功");

    }

    /**
     * 订单导出
     * @return mixed
     * @throws \think\Exception
     */
    public function orderExport()
    {
        $model = new OrderModel;
        if (!$model->orderExport($this->request->param())) {
            return $this->renderError([],$model->getError() ?: '操作失败');
        }
        return$model->orderExport($this->request->param());


    }


    /**
     * 商品订单
     * @return mixed
     * @throws \think\Exception
     */
    public function goods()
    {
        // 订单列表
        $model = new OrderModel;
        $list = $model->getAllList($this->request->param());
        return $this->renderSuccess($list,"查询成功");

    }

    /**
     * 当面付订单
     * @return mixed
     * @throws \think\Exception
     */
    public function face()
    {
        // 订单列表
        $model = new OrderModel;
        $list = $model->getAllList($this->request->param());
        return $this->renderSuccess($list,"查询成功");

    }


    /**
     * 爆品礼订单
     * @return mixed
     * @throws \think\Exception
     */
    public function gifts()
    {
        // 获取全部商品列表
        $model = new OrderModel;
        $list = $model->getAllList($this->request->param());
        return $this->renderSuccess($list,"查询成功");

    }



    /**
     * 储值订单
     * @return mixed
     * @throws \think\Exception
     */
    public function card()
    {
        // 订单列表
        $model = new OrderModel;
        $list = $model->getAllList($this->request->param());
        return $this->renderSuccess($list,"查询成功");

    }


    /**
     * 大额支付充值记录
     * @return mixed
     * @throws \think\Exception
     */
    public function large(){

        //获取该商户总额度
        $wxapp_id = $this->getWxappId();
        $money = Db::name('wxapp')->where(['wxapp_id'=>$wxapp_id])->field('money')->value('money');

        $startTime = $this->request->param('starttime');
        $endTime = $this->request->param('endtime');
        $search = $this->request->param('search');
        $paystatus = $this->request->param('pay_status');

        $flag=0;
        if(!empty($paystatus)){
            if($paystatus!=-1){
                $flag=1;
                $map['pay_status']  = ['=',$paystatus];
            }
        }
        if(!empty($search)){
            $flag=1;
            $map['order_no']  = ['like','%'.$search.'%'];
        }
        if(!empty($startTime) && !empty($endTime)){
            $flag=1;
            $startTime=strtotime($startTime." 00:00:00");
            $endTime=strtotime($endTime." 23:59:59");
            $map['create_time']  = ['between',[$startTime,$endTime]];
        }
        if(empty($startTime) && !empty($endTime)){
            $flag=1;
            $endTime = strtotime($endTime." 23:59:59");
            $map['create_time']  = ['<=',$endTime];
        }
        if(!empty($startTime) && empty($endTime)){
            $startTime=strtotime($startTime." 00:00:00");
            $flag=1;
            $map['create_time']  = ['>',$startTime];
        }
        if($flag==0){
            $map='1=1';
        }
        //订单列表
        $pagenum = $this->request->param('page');
        $page = isset($pagenum)&&is_numeric(input('post.page'))?intval($pagenum):1;
        $limit = 10;
        $offset = ($page-1) * $limit;
        $list = Db::name('large_order')
            ->limit($offset,$page)
            ->where($map)
            ->where(['is_delete'=>0]) 
            ->where('wxapp_id','=',$wxapp_id)
            ->order('create_time desc')
            ->paginate(10, false, [
                'query' => \request()->request()
            ])->each(function($item,$key){
                $item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
                $item['admin_user'] = Db::name('store_user')->where(['store_user_id'=>$item['admin_id']])->value('user_name');
                return $item;
            });

        return $this->renderSuccess(compact('money','list'),"查询成功");
    }


    /**
     * 删除大额支付订单
     * @return mixed
     * @throws \think\Exception
     */
    public function delete(){

        $param = $this->postData();

        if(!isset($param['order_id']) || isZeroNumeric($param['order_id'])){
            $this->renderError([],'参数错误');
        }

        $info = Db::name('large_order')->where(['order_id'=>$param['order_id']])->find();
        if(!$info){
            $this->renderError([],'订单不存在，请刷新重试');
        }
        if($info['pay_status']==20){
            $this->renderError([],'已支付订单禁止删除');
        }

        if(Db::name('large_order')->where(['order_id'=>$param['order_id']])->update(['is_delete'=>1])){
            $this->renderSuccess([],'删除成功');
        }else{
            $this->renderError([],'删除失败');
        }

    }


    /**
     * 大额提现
     * @return mixed
     * @throws \think\Exception
     */
    public function withdraw(){

        $money = input('post.money');

        if (!is_numeric($money)||!preg_match("/^[1-9][0-9]*$/" ,$money)||empty($money)) {
            return $this->renderError([],'请输入正确的提现金额(大于等于1的正整数)');
        }
        if ($money<1) {
            return $this->renderError([],'提现金额不能小于1');
        }
        //获取可提现金额
        $wxapp_id = $this->getWxappId();

        $wx_money = Db::name('wxapp')->where(['wxapp_id'=>$wxapp_id])->field('money')->value('money');
        if($money>$wx_money){
            return $this->renderError([],'提现金额不能大于总额度金额');
        }
        //更新总额度数据
        $update = Db::name('wxapp')->where(['wxapp_id'=>$wxapp_id])->setDec('money',$money);

        //写入提现数据
        $data['money'] = bcmul($money,'0.01',2);
        $data['apply_status']=10;
        $data['apply_time']=time();
        $data['wxapp_id']=$wxapp_id;
        $data['update_time']=0;
        $data['admin_id']=0;
        $data['back_reason']='';
        $data['is_delete']=0;
        $insert = Db::name('large_withdraw')->insertGetId($data);
        //写入操作日志表
        $logData=[
            'money'=>-1*$data['money'],
            'wxapp_id'=>$wxapp_id,
            'create_time'=>time(),
            'user_id'=>0,
            'admin_id'=>0,
            'type'=>3,
            'order_id'=>$insert,
            'is_delete'=>0,
            'store_user_id'=>$this->store_user_id
        ];
        $insertLog = Db::name('large_log')->insertGetId($logData);
        if($update&&$insert&&$insertLog){
            return $this->renderSuccess([],'申请成功，等待审核');
        }
        return $this->renderError([],'申请失败');
    }

    /**
     * 大额提现申请列表
     * @return mixed
     * @throws \think\Exception
     */
    public function apply(){

        $wxapp_id = $this->getWxappId();

        $startTime = $this->request->param('starttime');
        $endTime = $this->request->param('endtime');
        $apply_status = $this->request->param('apply_status');
        $flag=0;
        if(!empty($apply_status)){
            if($apply_status!=-1){
                $flag=1;
                $map['apply_status']  = ['=',$apply_status];
            }
        }

        if(!empty($startTime)&&!empty($endTime)){
            $flag = 1;
            $startTime = strtotime($startTime." 00:00:00");
            $endTime = strtotime($endTime." 23:59:59");
            $map['apply_time']  = ['between',[$startTime,$endTime]];
        }
        if(empty($startTime)&&!empty($endTime)){
            $flag = 1;
            $endTime = strtotime($endTime." 23:59:59");
            $map['apply_time']  = ['<=',$endTime];
        }
        if(!empty($startTime)&&empty($endTime)){
            $startTime = strtotime($startTime." 00:00:00");
            $flag=1;
            $map['apply_time']  = ['>',$startTime];
        }
        if($flag==0){
            $map='1=1';
        }
        //订单列表
        $pagenum = $this->request->param('page');
        $page = isset($pagenum)&&is_numeric(input('post.page'))?intval($pagenum):1;
        $limit = 10;
        $offset=($page-1) * $limit;
        $list = Db::name('large_withdraw')
            ->limit($offset,$page)
            ->where($map)
            ->where(['is_delete'=>0])
            ->where('wxapp_id','=',$wxapp_id)
            ->order('apply_time desc') 
            ->paginate(10, false, [
                'query' => \request()->request()
            ])->each(function($item,$key){
                $item['apply_time'] = date('Y-m-d H:i:s',$item['apply_time']);
                $item['admin'] = Db::name('admin_user')->where(['id'=>$item['admin_id']])->value('user_name')?:'';
                return $item;
            });
        return $this->renderSuccess($list,'查询成功');
    }




    /**
     * 分销订单
     * @return mixed
     * @throws \think\Exception
     */
    public function dealer()
    {
        // 获取全部商品列表
        $model = new DealerOrderModel;

        $list = $model->getFinanceList($this->request->param());

        return $this->renderSuccess($list,'查询成功');

    }


    /**
     * 分销订单导出
     * @return mixed
     * @throws \think\Exception
     */
    public function dealerExport()
    {
        // 获取全部商品列表
        $model = new DealerOrderModel;
        if (!$model->deaclerFinanceExport($this->request->param())) {
            return $this->renderError([],$model->getError() ?: '操作失败');
        }
        return $model->deaclerFinanceExport($this->request->param());
    }


    /**
     * 分红订单
     * @return mixed
     * @throws \think\Exception
     */
    public function bonus()
    {
        $model = new ReceiveModel();
        $wxapp_id = $this->getWxappId();

        $list = $model->getReceiveList($this->request->param(),$wxapp_id);

        return $this->renderSuccess($list,'查询成功');


    }


    /**
     * 分红记录导出
     * @return mixed
     * @throws \think\Exception
     */
    public function bonusExport()
    {
        $model = new ReceiveModel();
        $wxapp_id = $this->getWxappId();

        if (!$model->bonusFinanceExport($this->request->param(),$wxapp_id)) {
            return $this->renderError([],$model->getError() ?: '操作失败');
        }
        return $model->bonusFinanceExport($this->request->param(),$wxapp_id);

    }



}